package com.shop.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.shop.common.SetMeta;
import com.shop.common.page.GoodPage;
import com.shop.entity.Order;
import com.shop.entity.OrderAndGoods;
import com.shop.exception.OrderException;
import com.shop.mapper.OrderAndGoodsMapper;
import com.shop.mapper.OrderMapper;
import com.shop.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service("orderService")
public class OrderServiceImpl implements OrderService {
    @Autowired
    private OrderMapper orderMapper;

    @Autowired
    private OrderAndGoodsMapper orderAndGoodsMapper;

    /**
     * 订单数据列表
     * @param query
     * @param pagenum
     * @param pagesize
     * @param order
     * @return
     */
    @Override
    public SetMeta selectAll(String query, int pagenum, int pagesize, Order order) {
        Page<Order> orderPage = new Page<>(pagenum, pagesize);
        orderPage = orderMapper.selectPage(orderPage, Wrappers.lambdaQuery(Order.class).like(Order::getOrder_number, query)
                .eq(order.getUser_id() != null, Order::getUser_id, order.getUser_id()).eq(order.getPay_status() != null, Order::getPay_status, order.getPay_status())
                .eq(order.getIs_send() != null, Order::getIs_send, order.getIs_send()).eq(order.getOrder_fapiao_title() != null, Order::getOrder_fapiao_title, order.getOrder_fapiao_title())
                .eq(order.getOrder_fapiao_company() != null, Order::getOrder_fapiao_company, order.getOrder_fapiao_company()).eq(order.getOrder_fapiao_content() != null, Order::getOrder_fapiao_content, order.getOrder_fapiao_content())
                .eq(order.getConsignee_addr()!=null,Order::getConsignee_addr,order.getConsignee_addr()));
        SetMeta<GoodPage<List<Order>>> setMeta = new SetMeta<GoodPage<List<Order>>>();

        GoodPage<List<Order>> goodPage = new GoodPage<>();
        goodPage.setGoods(orderPage.getRecords());
        goodPage.setPagenum((int) orderPage.getCurrent());
        goodPage.setTotal((int) orderPage.getTotal());
        setMeta.setSuccess(goodPage,"获取成功");
        return setMeta;
    }

    /**
     * 修改订单状态
     * @param order
     * @return
     * @throws OrderException
     */
    @Override
    public SetMeta updateOrder(Order order) throws OrderException {
        //判断传入的参数是否有无效的值,如无效的枚举类型等等,以及因此产生的异常信息处理
        if (order.getOrder_id() == null
                || order.getOrder_id().equals("")
                || order.getCreate_time() != null
                || order.getOrder_fapiao_company() != null
                || order.getOrder_fapiao_content() != null
                || order.getOrder_fapiao_title() != null
                || order.getUpdate_time() != null
                || order.getConsignee_addr() != null
                || order.getGoods() != null
                || order.getTrade_no() != null
                || order.getUser_id() != null
        ) throw new OrderException("参数传递错误");


        int update = orderMapper.update(order, Wrappers.lambdaUpdate(Order.class).eq(Order::getOrder_id, order.getOrder_id()));
        SetMeta<Order> setMeta=new SetMeta<>();
        if (update!=0){
            order=selectOrder(order.getOrder_id());
            setMeta.setSuccess(order,"修改成功");
        }else{
            setMeta.setError("修改失败");
        }
        return setMeta;
    }

    /**
     * 查看订单详情
     *
     * @param id
     * @return
     * @throws OrderException
     */
    @Override
    public Order selectOrder(Integer id) throws OrderException {
        Order order = orderMapper.selectById(id);
        if (order == null) {
            throw new OrderException("该订单不存在");
        }
        List<OrderAndGoods> orderWithGoods = orderAndGoodsMapper.selectList(Wrappers.lambdaQuery(OrderAndGoods.class).eq(OrderAndGoods::getOrder_id, order.getOrder_id()));
        if (orderWithGoods != null) {
            order.setGoods(orderWithGoods);
        }
        return order;
    }

    /**
     * 修改地址
     * @param id
     * @param consignee_addr
     * @return
     * @throws OrderException
     */
    @Override
    public SetMeta updateOrderAddress(Integer id, String consignee_addr) throws OrderException {
        Order order=selectOrder(id);
        order.setConsigneeAddr(consignee_addr);
        int update = orderMapper.updateById(order);
        SetMeta<Order> setMeta=new SetMeta<>();
        if (update!=0){
            setMeta.setSuccess(order,"修改地址成功");
        }else{
            setMeta.setError("修改地址失败");
        }
        return setMeta;
    }
}
